package oo_day04;

public class Test {
    public static void main(String[] args) {
        //Animal o = new Animal();//抽象类不能被实例化
        Animal[] animals = new Animal[4];//创建Animal数组对象
        animals[0] = new Dog("小黑",1,"黑色");//向上造型
        animals[1] = new Dog("小白",2,"白色");
        animals[2] = new Chick("小黄",3,"黄色");
        animals[3] = new Fish("小灰",4,"灰色");
        for (int i = 0; i < animals.length; i++) {//遍历所以动物
            System.out.println(animals[i].name);//输出每个动物额名字
            System.out.println(animals[i].age);
            animals[i].eat();//每个动物吃饭
            animals[i].drink();

                            //判断是否是Dog这个类的实例
            if (animals[i] instanceof Dog) {
                Dog dog = (Dog) animals[i];//是的话就强转
                dog.lookHome();
               // dog.swim();重复了,可以直接在游泳里面判断
            }
            if(animals[i] instanceof Chick){
                Chick chick = (Chick)animals[i];
                chick.egg();
            }
            if (animals[i] instanceof Fish){
                Fish fish = (Fish)animals[i];
                //fish.swim();重复了,可以直接在游泳里面判断
            }
             if (animals[i] instanceof Swim){
                Swim s = (Swim)animals[i];
                s.swim();
            }
        }


        /*
        //演示向上造型(多态)的第2点应用;
        Master master = new Master();
        Dog dog = new Dog("小黑",1,"黑色");
        Chick chick = new Chick("小鸡",2,"黄色");
        Fish fish = new Fish("小鱼",3,"白色");
        master.feed(dog);//在传参的同时,系统自动做了向上造型
        master.feed(chick);
        master.feed(fish);

         */





        /*
        Dog[]dogs = new Dog[3];
        dogs[0] = new Dog("小白",1,"白");
        dogs[1] = new Dog("小黑",2,"黑");
        dogs[2] = new Dog("小黄",3,"黄色");
        for (int i = 0; i < dogs.length; i++) {
            System.out.println(dogs[i].name);
            System.out.println(dogs[i].age);
            System.out.println(dogs[i].color);
            dogs[i].drink();
            dogs[i].eat();
        }
        Chick[]chicks = new Chick[3];
        chicks[0] = new Chick("小鸡",1,"白色");
        chicks[1] = new Chick("小小鸡",2,"黑色");
        chicks[2] = new Chick("小小小鸡",3,"灰色");
        for (int i = 0; i < chicks.length; i++) {
            System.out.println(chicks[i].name);
            chicks[i].eat();
            chicks[i].drink();
        }
        Fish[]fish = new Fish[2];
        fish[0] = new Fish("小鱼",1,"白色");
        fish[1] = new Fish("小小鱼",2,"黑色");
        for (int i = 0; i < fish.length; i++) {
            System.out.println(fish[i].name);
            fish[i].swim();
        }

         */


    }
}

